<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>generateUntil</title>
</head>

<body>
    <script>
        const generateUntil = function* (seed, condition, next) {
            let val = seed;
            let nextSeed = null;
            while (!condition(val)) {
                nextSeed = yield val;
                val = next(val, nextSeed);
            }
            return val;
        };
        document.writeln(`${[
            ...generateUntil(
                1,
                v => v > 5,
                v => ++v
            )
        ]}`)
    </script>
</body>

</html>